package com.vibration.baseinfo.service.impl;

import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.List;

import com.vibration.socket.domain.DataCollection;
import com.vibration.socket.util.SocketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vibration.baseinfo.mapper.CzGyroscopeDataMapper;
import com.vibration.baseinfo.domain.CzGyroscopeData;
import com.vibration.baseinfo.service.ICzGyroscopeDataService;

/**
 * 陀螺仪数据Service业务层处理
 * 
 * @author wu
 * @date 2022-08-11
 */
@Service
public class CzGyroscopeDataServiceImpl implements ICzGyroscopeDataService 
{
    @Autowired
    private CzGyroscopeDataMapper czGyroscopeDataMapper;

    /**
     * 查询陀螺仪数据
     * 
     * @param id 陀螺仪数据主键
     * @return 陀螺仪数据
     */
    @Override
    public CzGyroscopeData selectCzGyroscopeDataById(Long id)
    {
        return czGyroscopeDataMapper.selectCzGyroscopeDataById(id);
    }

    /**
     * 查询陀螺仪数据列表
     * 
     * @param czGyroscopeData 陀螺仪数据
     * @return 陀螺仪数据
     */
    @Override
    public List<CzGyroscopeData> selectCzGyroscopeDataList(CzGyroscopeData czGyroscopeData)
    {
        return czGyroscopeDataMapper.selectCzGyroscopeDataList(czGyroscopeData);
    }

    /**
     * 新增陀螺仪数据
     * 
     * @param czGyroscopeData 陀螺仪数据
     * @return 结果
     */
    @Override
    public int insertCzGyroscopeData(CzGyroscopeData czGyroscopeData)
    {
        return czGyroscopeDataMapper.insertCzGyroscopeData(czGyroscopeData);
    }

    /**
     * 修改陀螺仪数据
     * 
     * @param czGyroscopeData 陀螺仪数据
     * @return 结果
     */
    @Override
    public int updateCzGyroscopeData(CzGyroscopeData czGyroscopeData)
    {
        return czGyroscopeDataMapper.updateCzGyroscopeData(czGyroscopeData);
    }

    /**
     * 批量删除陀螺仪数据
     * 
     * @param ids 需要删除的陀螺仪数据主键
     * @return 结果
     */
    @Override
    public int deleteCzGyroscopeDataByIds(Long[] ids)
    {
        return czGyroscopeDataMapper.deleteCzGyroscopeDataByIds(ids);
    }

    /**
     * 删除陀螺仪数据信息
     * 
     * @param id 陀螺仪数据主键
     * @return 结果
     */
    @Override
    public int deleteCzGyroscopeDataById(Long id)
    {
        return czGyroscopeDataMapper.deleteCzGyroscopeDataById(id);
    }

    @Override
    public CzGyroscopeData getGyroscopeData(String code) {
        //获取上一条数据
        DataCollection dataCollection = SocketUtil.gyroscopeSocketMap.get(code);
        if(dataCollection!=null){
            CzGyroscopeData data = dataCollection.getGyroscopeData();
            new pollThread(dataCollection);
            return data;
        }

        return null;
    }

    /****************************发送报文开始********************************/
    private class pollThread implements Runnable {
        private DataCollection dataCollection;
        public pollThread(DataCollection dc){
            dataCollection = dc;
            Socket socket = dataCollection.getSocket();
            if(socket!=null && !socket.isClosed()) {
                new Thread(this).start();
            }
        }
        public void run() {
            Socket socket = dataCollection.getSocket();
            if(socket!=null && !socket.isClosed()){
                //发送指令
                try {
                    SocketUtil.sendData("7704000408", socket.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /****************************发送报文结束********************************/


    /***
     * 查询最近一小时陀螺仪数据
     * @param gyroscopeCode
     * @return
     */
    @Override
    public List<CzGyroscopeData> selectCzGyroscopeDataByCodeOneHour(String gyroscopeCode) {
        return czGyroscopeDataMapper.selectCzGyroscopeDataByCodeOneHour(gyroscopeCode);
    }
}
